package ml

import org.apache.spark.ml.feature.LabeledPoint
import org.apache.spark.ml.linalg.Vectors
import org.apache.spark.ml.regression.LinearRegression
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SparkSession

object PointsLine {
  def main(args: Array[String]): Unit = {
    val spark: SparkSession = SparkSession
      .builder()
      .master("local")
      .appName("point")
      .getOrCreate()

    import spark.implicits._
    val context = spark.sparkContext
    val src = context.textFile("spark/data/points.txt")
    val feartureRDD = src.map(line => {
      val sp = line.split(",")
      val name = sp(0).toDouble
      val fearture = sp(1).toDouble
      LabeledPoint(fearture, Vectors.dense(name))
    })
    val regression = new LinearRegression()
    val model = regression.fit(feartureRDD.toDF())
    println(model.intercept)
    println(model.coefficients)

    println(model.predict(Vectors.dense(4)))


  }

}
